﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SceneFrame
{
    /// <summary>
    /// 场景管理器
    /// </summary>
    public class SceneManager
    {
        private List<Scene> _SceneSet = new List<Scene>();

        private SceneManager() { }

        private static SceneManager _SceneManager = new SceneManager();

        public static SceneManager GetInstance() { return _SceneManager; }

        public static long TimeNow { get { return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000; } }

        /// <summary>
        /// 托管一个场景
        /// </summary>
        /// <param name="scene"></param>
        /// <returns></returns>
        public void Add(Scene scene)
        {
            lock (_SceneSet)
                _SceneSet.Add(scene);
        }

        public void Replace(Scene new_one, Scene old_one)
        {
            lock (_SceneSet)
            {
                _SceneSet.Remove(old_one);
                _SceneSet.Add(new_one);
            }
        }

        /// <summary>
        /// 删除一个场景
        /// </summary>
        /// <param name="scene"></param>
        /// <returns></returns>
        public bool Remove(Scene scene)
        {
            lock (_SceneSet)
                return _SceneSet.Remove(scene);
        }

        /// <summary>
        /// 获取全部的场景
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Scene> GetAll()
        {
            lock (_SceneSet)
                return new List<Scene>(_SceneSet);
        }

        /// <summary>
        /// 清理掉所有的场景
        /// </summary>
        public void Clear()
        {
            lock (_SceneSet)
                _SceneSet.Clear();
        }

        ///// <summary>
        ///// 删除一个场景
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public int Remove(long id)
        //{
        //    return _SceneSet.RemoveWhere(n => n.Id == id);
        //}

        /// <summary>
        /// 尝试触发事件
        /// </summary>
        /// <param name="trigger"></param>
        public void Trigger(Trigger trigger)
        {
            lock (_SceneSet)
            {
                foreach (var item in _SceneSet)
                    item.Trigger(trigger);
            }
        }
    }
}
